package com.cas.util;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;

/**
 * 简单验证码生成
 * 参考连接 https://www.cnblogs.com/lr393993507/p/5455935.html
 *
 * @version 0.1
 * @auth .
 * @time 2018/2/25 16:10
 * @since 0.1
 */
public class CodeDrawUtil {

    private Graphics2D gp2d;
    private String captcha;

    private static Random random = new Random();

    public static final int IMG_WIDTH = 100;
    public static final int IMG_HEIGHT = 30;

    private static final String[] code = new String[]
            {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public CodeDrawUtil(Graphics2D gp2d) {
        this.gp2d = gp2d;
    }

    public String getCaptcha() {
        if(captcha == null){
            throw new NullPointerException("验证码没有生成");
        }

        return captcha;
    }

    /*
     * 获得颜色
     */
    private Color getRandColor(int r, int g, int b) {
        return new Color(random.nextInt(r), random.nextInt(g), random.nextInt(b));
    }

    /*
     * 绘制图片
     */
    public CodeDrawUtil drawImg(int width, int height) {
        gp2d.fillRect(0, 0, width, height);
        gp2d.setColor(Color.white);
        return this;
    }

    /*
     * 绘制噪点
     *
     */
    public CodeDrawUtil drawEclipse(int number) {
        for (int i = 0; i < number; i++) {
            gp2d.setPaint(this.getRandColor(255, 255, 255));
            int x = random.nextInt(100);
            int y = random.nextInt(50);
            double alpha = random.nextDouble();
            gp2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) alpha));// 1.0f为透明度 ，值从0-1.0，依次变得不透
            gp2d.fill(new Ellipse2D.Double(x, y, 5, 5));
        }

        return this;
    }

    /*
     * 绘制干扰线
     */
    public CodeDrawUtil drawLine(int number) {
        for (int i = 0; i < number; i++) {
            int x = random.nextInt(100);
            int y = random.nextInt(50);
            int xl = random.nextInt(100);
            int yl = random.nextInt(50);
            double alpha = random.nextDouble();

            // 1.0f为透明度 ，值从0-1.0，依次变得不透
            gp2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) alpha));
            gp2d.setPaint(this.getRandColor(255, 255, 255));
            gp2d.drawLine(x, y, x + xl, y + yl);
        }
        return this;
    }

    /**
     * 绘制验证码
     *
     * @param number 验证码数量
     * @return
     */
    public CodeDrawUtil drawFont(int number) {
        StringBuilder strCode = new StringBuilder();
        for (int i = 0; i <  number; i++) {
            double alpha = random.nextDouble()+0.6;
            gp2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ((float) alpha) > 1.0F ? 1.0F : ((float) alpha)));// 1.0f为透明度 ，值从0-1.0，依次变得不透

            Font font = new Font("Times New Roman", Font.BOLD, 30);
            gp2d.setFont(font);
            gp2d.setColor(getRandColor(random.nextInt(101)+1, random.nextInt(111)+1, random.nextInt(121)+1));

            int x = (int) (20 * (i + 1) - random.nextInt(10));
            int y = (int) (random.nextInt(10) + 20);
            gp2d.translate(random.nextInt(3), random.nextInt(1)); //相对位置偏移

            int index = random.nextInt(code.length);

            String temp = code[index];
            strCode.append(temp);
            gp2d.drawString(temp, x, y);
        }

        captcha = strCode.toString();
        return this;
    }

    public void dispose(){
        gp2d.dispose();
        gp2d = null;
    }


    /**
     * 获取当前系统字体
     *
     * @return
     */
    private String getFontFamilyName() {
        GraphicsEnvironment gEnv =
                GraphicsEnvironment.getLocalGraphicsEnvironment();

        String[] s = gEnv.getAvailableFontFamilyNames();

        int index = s.length;
        return s[random.nextInt(index)];
    }

    private String getFontStyle() {
        String[] styleNames = {"Plain", "Bold", "Italic", "Bold Italic"};
        int index = styleNames.length;
        return styleNames[random.nextInt(index)];
    }

}
